package com.dhy.springboot.controller;

import com.dhy.springboot.entity.Book;
import com.dhy.springboot.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
public class MyController {

    @Autowired
    BookMapper bookMapper;

    @GetMapping("/queryAllBook")
    public List<Book> queryAllBook(){
        List<Book> books = bookMapper.queryAllBook();
        return books;
    }

    @GetMapping("/queryBookById")
    public Book queryBookById(int id){
        Book book = bookMapper.queryBookById(id);
        return book;
    }

    @PostMapping("/insertBook")
    public int insertBook(Book book){
        int i = bookMapper.insertBook(book);
        return i;
    }

    @PutMapping("/updateBook")
    public int updateBook(Book book){
        int i = bookMapper.updateBook(book);
        return i;
    }

    @DeleteMapping("/deleteBook")
    public int deleteBook(int id){
        int i = bookMapper.deleteBook(id);
        return i;
    }

    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/query")
    public Map<String, Object> hello(){
        List<Map<String, Object>> list =  jdbcTemplate.queryForList("SELECT * FROM `book`");

        return list.get(0);
    }

    @GetMapping("/query2")
    public List hello2(){
        List list =  jdbcTemplate.queryForList("SELECT * FROM `book`");

        return list;
    }

    @GetMapping("/allBook")
//    @ResponseBody
    public List<Book> allBook(){
//        List<Book> list = new ArrayList<>();
//        list.add(new Book(1001,"java",98,"从入门到精通"));
//        list.add(new Book(1002,"python",15,"从入门到精通"));
//        list.add(new Book(1003,"go",225,"从入门到精通"));

        List list = jdbcTemplate.queryForList("SELECT * FROM `book`");


        return list;
    }


}
